import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BasePage } from '../../basePage';
import { StorageService } from './../../../providers/StorageService';
import { UserService } from './../../../providers/UserService';

/**
 * Generated class for the ManagergoodsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-managergoods',
  templateUrl: 'managergoods.html',
})
export class ManagergoodsPage extends BasePage {

  userinfo = {};
  addresslist = [];
  blank:boolean;
  isNotFromOrder:boolean;

  constructor(injector: Injector, public userService: UserService, public stc: StorageService) {
    super(injector);
  }

  ionViewDidLoad() {
    this.userinfo = this.stc.read("userinfo");
    console.log(this.userinfo);
    this.getAddressListAPI();
    console.log('ionViewDidLoad ManagergoodsPage lixiang');
    console.log(this.addresslist)
    if(this.addresslist.length==0){
      this.blank=true;
    }else{
      this.blank=false;
    }

    /*
      this.isNotFromOrder 控制 返回图片是否显示  
      true 表示显示 
      false表示不显示
    */
    
    if(this.navParams.get('isFromOrder')!=null)
    this.isNotFromOrder = !this.navParams.get('isFromOrder');
    else this.isNotFromOrder = true;
  }


  /**打开地址编辑页面 */
  goodseditCLI(item) {
    item['oper'] = 2;
    item['title'] = "编辑收货地址";
    console.log(item);
    let modal = this.modalCtrl.create('GoodseditPage', item);
    modal.onDidDismiss(data => {
      if (data) {
        this.getAddressListAPI();
      }
      return;
    });
    modal.present();
  }


  /**删除地址 */
  async delAddressAPI(item){    
    let param={'address_id':item['id'],'user_id':item['user_id']};
    let result = await this.userService.delAddressAPI(param);
    console.log(result);
    if(result.code == 1){
      this.getAddressListAPI();
    }else{
      this.showAlert(result.msg)
    }
  }


  /**设为默认地址 */
  async editDefaultAddressAPI(item){
    let param={'address_id':item['id'],'user_id':item['user_id']};
    let result = await this.userService.editDefaultAddressAPI(param);
    if(result.code == 1){
      this.getAddressListAPI();
    }else{
      this.showAlert(result.msg)
    }
  }


  /**新增收获地址 */
  addAddressCLI() {
    let modal = this.modalCtrl.create('GoodseditPage', { "oper": 1, "title": "新增收货地址" });
    modal.onDidDismiss(data => {
      if (data) {
        this.getAddressListAPI();
      }
      return;
    });
    modal.present();
  }

  dismissCLI() {
    this.navCtrl.pop()
  }


  /**获取地址列表 */
  async getAddressListAPI() {
    let param = { 'user_id': this.userinfo['user_id'] };
    let loader = this.loadingCtrl.create({
      content: "",
    });
    loader.present();
    let result = await this.userService.getAddressListAPI(param);
    loader.dismiss();
    console.log(result);
    if (result.code == 1) {
      this.addresslist = result.data;
      this.blank=false;
      console.log(this.addresslist);
    } else {
      this.blank=true;
      this.showAlert(result.msg);
    }
  }


}
